package andriod.action.daily;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import andriod.service.ArdDailyService;
import andriod.util.StringProcessor;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.ODaily;
import com.jcrj.entity.OUser;
import com.jcrj.service.PersonalofficeService;
import com.jcrj.util.DateProcessor;

public class ArdDailyAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private ArdDailyService ardDailyService;
	private PersonalofficeService personalofficeService;
	private ODaily daily;
	private String[] dailyIds;
	private int dailyId;
	private String userName;
	private String name;
	private long pageCount;
	private long totalCount;

	public String emplyeeDailyManage() throws IOException {
		OUser user = (OUser) super.getSession().get("userInfo");
		
		String users = this.personalofficeService.getUserEmplyee(user.getId()
				.toString());
//		long totalCount = this.personalofficeService.countDailyEmplyee(users,
//				this.name, this.userName, super.getStartDate(),
//				super.getEndDate()).longValue();
//		long pageCount = ((totalCount - 1L) / 10L + 1L);
//		if (super.getCurrentPage() > pageCount) {
//			super.setCurrentPage(Long.valueOf(pageCount).intValue());
//		}
		
		this.totalCount = this.personalofficeService.countDailyEmplyee(users,
				this.name, this.userName, super.getStartDate(),
				super.getEndDate()).longValue();
		this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		
		List<ODaily> dailyList = this.personalofficeService.emplyeeDailyList(users,
				this.name, this.userName, super.getStartDate(),
				super.getEndDate(), super.getCurrentPage());
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class, new DateProcessor());
		
		JSONArray array = JSONArray.fromObject(dailyList,config);
		JSONObject obj = new JSONObject();
		
		obj.put("totalCount", this.totalCount);
		obj.put("pageCount", this.pageCount);
		obj.put("currentPage", super.getCurrentPage());
		obj.put("list", array);
		
		HttpServletResponse response = super.getResponse();
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(obj.toString());
		return null;
	}

	public String getDailyList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			
			this.totalCount = this.personalofficeService.countDaily(user.getId().intValue(), this.name, super.getStartDate(), super.getEndDate()).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			
//			List<ODaily> list = this.ardDailyService.getDailyList(user.getId());
			
			List<ODaily> list = this.personalofficeService.getDaily(user.getId().intValue(), this.name, super.getCurrentPage(), super.getStartDate(), super.getEndDate());
			
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
//			JSONArray obj = JSONArray.fromObject(list, config);
			
			JSONObject obj = new JSONObject();
			JSONArray array = JSONArray.fromObject(list, config);
			
			obj.put("totalCount", this.totalCount);
			obj.put("pageCount", this.pageCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);
			
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String toDailyAdd() {
		try {
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String dailyAdd() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.daily.setUserId(user.getId());
			this.daily.setUserName(user.getName());
			this.personalofficeService.addDaily(this.daily);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String toDailyUpdate() {
		try {
			ODaily od = this.personalofficeService.getDailyNew(this.dailyId);
			JSONObject obj = new JSONObject();
			JsonConfig config = new JsonConfig();
//			config.registerJsonValueProcessor(String.class, new StringProcessor());
			DateProcessor dateProcessor = new DateProcessor();
			config.registerJsonValueProcessor(Date.class, dateProcessor);
			config.registerJsonValueProcessor(java.sql.Date.class, dateProcessor);
			JSONObject odJson = JSONObject.fromObject(od,config);
			obj.put("object", odJson);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String dailyUpdate() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.daily.setUserId(user.getId());
			this.daily.setUserName(user.getName());
			this.personalofficeService.updateDalily(this.daily);
			JSONObject obj = new JSONObject();
			obj.put("flag", true);
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteDaily() {
		try {
			this.personalofficeService.deleteDaily(this.dailyIds);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e1) {
			e1.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public ArdDailyService getArdDailyService() {
		return this.ardDailyService;
	}

	public void setArdDailyService(ArdDailyService ardDailyService) {
		this.ardDailyService = ardDailyService;
	}

	public PersonalofficeService getPersonalofficeService() {
		return this.personalofficeService;
	}

	public void setPersonalofficeService(
			PersonalofficeService personalofficeService) {
		this.personalofficeService = personalofficeService;
	}

	public String[] getDailyIds() {
		return this.dailyIds;
	}

	public void setDailyIds(String[] dailyIds) {
		this.dailyIds = dailyIds;
	}

	public int getDailyId() {
		return this.dailyId;
	}

	public void setDailyId(int dailyId) {
		this.dailyId = dailyId;
	}

	public void setDaily(ODaily daily) {
		this.daily = daily;
	}

	public ODaily getDaily() {
		return this.daily;
	}
	
	
	public long getPageCount() {
		return this.pageCount;
	}
	
	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}
	
	public long getTotalCount() {
		return this.totalCount;
	}
	
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
	
	public String getName(){
		return this.name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getUserName(){
		return this.userName;
	}
	public void setUserName(String userName){
		this.userName = userName;
	}
	
}